package top.jiangqiang.qianyi.web.controller.admin;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.jiangqiang.qianyi.business.model.request.*;
import top.jiangqiang.qianyi.business.model.response.AdminUserDetailResponse;
import top.jiangqiang.qianyi.business.model.response.AdminUserListResponse;
import top.jiangqiang.qianyi.business.model.response.AdminUserPageResponse;
import top.jiangqiang.qianyi.business.service.AdminUserService;
import top.jiangqiang.qianyi.common.auth.service.AuthService;
import top.jiangqiang.qianyi.common.base.controller.BaseController;
import top.jiangqiang.qianyi.common.base.model.response.PageResult;
import top.jiangqiang.qianyi.common.base.model.response.Result;
import top.jiangqiang.qianyi.common.constants.GlobalConstants;
import top.jiangqiang.qianyi.common.constants.log.SysLogType;
import top.jiangqiang.qianyi.common.log.annotations.Log;

import java.util.List;

/**
 * <p>
 * 后台用户账号 前端控制器
 * </p>
 *
 * @author JiangQiang
 * @since 2024-06-08
 */
@Tag(name = "后台用户账号接口", description = "")
@RestController
@RequiredArgsConstructor
@RequestMapping(value = {GlobalConstants.AdminRequestURLConstants.ADMIN_USER_CONTROLLER_PREFIX})
public class AdminUserController extends BaseController {
    private final AdminUserService adminUserService;
    private final AuthService authService;

    @GetMapping("/page")
    @Operation(summary = "后台用户账号分页查询")
    @Log(value = "后台用户账号分页查询", type = SysLogType.PAGE)
    public Result<PageResult<AdminUserPageResponse>> page(@Validated @ParameterObject AdminUserPageRequest request) {
        return adminUserService.page(request);
    }

    @GetMapping("/list")
    @Operation(summary = "后台用户账号全表查询")
    @Log(value = "后台用户账号全表查询", type = SysLogType.LIST)
    public Result<List<AdminUserListResponse>> list(@Validated @ParameterObject AdminUserListRequest request) {
        return adminUserService.list(request);
    }

    @PostMapping
    @Operation(summary = "后台用户账号新增")
    @Log(value = "后台用户账号新增", type = SysLogType.ADD)
    public Result<Void> save(@Validated AdminUserSaveRequest request) {
        return adminUserService.add(request);
    }

    @PutMapping
    @Operation(summary = "后台用户账号修改")
    @Log(value = "后台用户账号修改", type = SysLogType.UPDATE)
    public Result<Void> update(@Validated AdminUserUpdateRequest request) {
        return adminUserService.update(request);
    }

    @GetMapping("/{id}")
    @Operation(summary = "后台用户账号详情")
    @Log(value = "后台用户账号详情", type = SysLogType.DETAIL)
    public Result<AdminUserDetailResponse> detail(@NotNull(message = "ID不能为空") @PathVariable("id") Long id) {
        return adminUserService.detail(id);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "后台用户账号删除")
    @Log(value = "后台用户账号删除", type = SysLogType.DELETE)
    public Result<Void> delete(@NotNull(message = "ID不能为空") @PathVariable("id") Long id) {
        return adminUserService.delete(id);
    }

    @PostMapping("/login")
    @Operation(summary = "后台用户登录")
    @Log(value = "后台用户登录", type = SysLogType.LOGIN)
    public Result<String> login(@Validated @RequestBody AdminUserLoginRequest loginRequest) {
        return adminUserService.login(loginRequest);
    }

}
